package com.gxa.saas.controller;

import com.gxa.saas.pojo.*;
import com.gxa.saas.service.OrderAddService;
import com.gxa.saas.service.OrderService;
import com.gxa.saas.service.StorageService;
import com.gxa.saas.service.StoreHouseOutService;
import com.gxa.saas.utils.JsonResult;
import com.sun.org.apache.bcel.internal.generic.NEW;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.swing.text.DateFormatter;
import javax.xml.crypto.Data;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/salesAdd")
@CrossOrigin
public class SalesAddController {

    @Autowired
    OrderService orderService;

    @Autowired
    OrderAddService orderAddService;
    @Autowired
    StoreHouseOutService storeHouseOutService;
    @Autowired
    StorageService storageService;
//    @Autowired
//    StorageService storageService;
    @ApiOperation(value = "编辑提交订单",produces = "query")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "order",value = "添加内容 2022-06-15 11:55:38 时间格式",dataType = "Order",paramType = "body"),

    })
    @PostMapping("/add")
    public Map insert(@RequestBody Order order){
//        String createTime = String.valueOf(order.getCreateTime());
//        DateTimeFormatter formatter=DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
//        LocalDateTime parse=LocalDateTime.parse(createTime,formatter);
//        parse.
//        order.setCreateTime();
        order.setCreateTime(new Date());
        Integer integer = orderAddService.updateOrder(order);
        StoreHouseOut storeHouseOut = new StoreHouseOut();
        storeHouseOut.setId(order.getId());
        storeHouseOut.setCreateDate(order.getCreateTime());
        storeHouseOut.setDepot(order.getWareHouse());
        storeHouseOut.setOName(order.getSalesman());
        String amount= String.valueOf(order.getMoney());
        storeHouseOut.setAmount(amount);
        storeHouseOut.setTime(new Date());
        storeHouseOut.setUName(order.getSalesman());
        storeHouseOut.setAName(order.getSalesman());
        storeHouseOut.setPayType(order.getPaymentTypes());
        storeHouseOut.setState("1");
        StoreHouseOut storeHouseOut1 = storeHouseOutService.selectById(order.getId());
        if (storeHouseOut1!=null){
            storeHouseOutService.updateByOut(storeHouseOut);
        }else {
            storeHouseOutService.addOutList(storeHouseOut);
        }

        JsonResult jsonResult=new JsonResult();
        jsonResult.setCount("200");
        jsonResult.setCount("提交成功");
        return jsonResult.getMap();
    }




    @ApiOperation(value = "搜索查询库存明细",produces = "query")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "模糊查询",paramType = "query"),
            @ApiImplicitParam(name = "page",value = "页数",paramType = "query"),
            @ApiImplicitParam(name = "size",value = "每页记录数",paramType = "query"),

    })
    @PostMapping("/selectSorage")
    public Map selectSorage(String name, Integer page ,Integer size){

        List<Storage> list = orderAddService.selectByName(name, page, size);
        List<Storage> list1 = orderAddService.selectAll();
        int count= list1.size();
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCount("200");
        jsonResult.setCount("查询成功");
        jsonResult.set("count",count);
        jsonResult.setData(list);
        return jsonResult.getMap();
    }
    @ApiOperation(value = "进页面展示库存明细",produces = "query")
    @ApiImplicitParams({

            @ApiImplicitParam(name = "page",value = "页数",paramType = "query"),
            @ApiImplicitParam(name = "size",value = "每页记录数",paramType = "query"),

    })
    @PostMapping("/selectByList")
    public Map selectByList(HttpServletRequest request, @RequestParam(value = "page",required = true)Integer page,
                            @RequestParam(value = "size",required = false,defaultValue = "10") Integer size){
        Enumeration<String> parameterNames = request.getParameterNames();

        List<Storage> list = orderAddService.selectByList(page, size);
        List<Storage> list1 = orderAddService.selectAll();
        int count= list1.size();
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCount("200");
        jsonResult.setCount("查询成功");
        jsonResult.set("count",count);
        jsonResult.setData(list);
        return jsonResult.getMap();
    }
    @ApiOperation(value = " 预览--商品明细查询 进一个接口",produces = "query")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId",value = "对应订单编号id--10018有数据",dataType = "int",paramType = "query"),
        

    })
    @PostMapping("/selectByGoodsXqList")
        public Map selectByGoodsXqList(Integer orderId){
        Order order = orderService.selectByOrderId(orderId);
        List<GoodsXq> goodsXqs = orderAddService.selectByGoodsXq(orderId);
        Integer count=goodsXqs.size();
        Double sumPrice=0.00;
        for (int i = 0; i <goodsXqs.size()-1 ; i++) {
            Double money = goodsXqs.get(i).getMoney();
            sumPrice=sumPrice+money;

        }
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCount("200");
        jsonResult.setCount("查询成功");
        jsonResult.setData(goodsXqs);
        jsonResult.set("count",count);
        jsonResult.set("sumPrice",sumPrice);
        jsonResult.set("order",order);
        return jsonResult.getMap();
    }
    @ApiOperation(value = "搜索结果添加接口",produces = "query")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "搜索结果添加对应的商品id",dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "orderId",value = "对应订单编号id",dataType = "int",paramType = "query"),

    })
    @PostMapping("/addGoods")
    public Map insertGoods(Integer orderId, Integer id){
        System.out.println("有请求进来了");
        Storage storage = orderAddService.selectById(id);
        StringBuilder big=new StringBuilder();
        StringBuilder small=new StringBuilder();
        GoodsXq goodsXq = new GoodsXq();

        String bigUnit=storage.getBigUnit();
        String bigPrice= String.valueOf(storage.getBigPrice());

        big.append(bigPrice);
        big.append("/");
        big.append(bigUnit);
        String smallUnit=storage.getSmallUnit();
        String smallPrice= String.valueOf(storage.getSmallPrice());
        small.append(smallPrice);
        small.append("/");
        small.append(smallUnit);
        goodsXq.setOrderId(orderId);
        goodsXq.setName(storage.getName());
        goodsXq.setBigUnit(big.toString());
        goodsXq.setSmallUnit(small.toString());
        Double bigPrice1 = storage.getBigPrice();
        Double smallPrice1 = storage.getSmallPrice();
        goodsXq.setBigCount(1);
        goodsXq.setSmallCount(1);
        goodsXq.setGoodsType("正常");

        goodsXq.setMoney(bigPrice1*1+smallPrice1*1);
        goodsXq.setProtectedDate(new Date());

        Integer insert = orderAddService.insert(goodsXq);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCount("200");
        jsonResult.setCount("添加成功");
        return jsonResult.getMap();
    }
    @ApiOperation(value = "删除操作",produces = "query")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "删除的id",paramType = "query"),

    })
    @PostMapping("/delete")
    public Map delete(Integer id){
        orderAddService.delete(id);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCount("200");
        jsonResult.setCount("删除成功");
        return jsonResult.getMap();
    }
    @ApiOperation(value = "搜索结果赠品接口",produces = "query")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "搜索结果添加对应的商品id",dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "orderId",value = "对应订单编号id",dataType = "int",paramType = "query"),

    })
    @PostMapping("/addGoodsGiveaway")
    public Map insertGoodsGiveaway(Integer orderId, Integer id){

        Storage storage = orderAddService.selectById(id);
        StringBuilder big=new StringBuilder();
        StringBuilder small=new StringBuilder();
        GoodsXq goodsXq = new GoodsXq();

        String bigUnit=storage.getBigUnit();
        String bigPrice= String.valueOf(storage.getBigPrice());

        big.append(bigPrice);
        big.append("/");
        big.append(bigUnit);
        String smallUnit=storage.getSmallUnit();
        String smallPrice= String.valueOf(storage.getSmallPrice());
        small.append(smallPrice);
        small.append("/");
        small.append(smallUnit);
        goodsXq.setOrderId(orderId);
        goodsXq.setName(storage.getName());
        goodsXq.setBigUnit(big.toString());
        goodsXq.setSmallUnit(small.toString());
        Double bigPrice1 = storage.getBigPrice();
        Double smallPrice1 = storage.getSmallPrice();
        goodsXq.setBigCount(1);
        goodsXq.setSmallCount(1);
        goodsXq.setGoodsType("赠品");


        goodsXq.setProtectedDate(new Date());
        goodsXq.setMoney(0.00);
        Integer insert = orderAddService.insert(goodsXq);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCount("200");
        jsonResult.setCount("添加成功");
        return jsonResult.getMap();
    }

    @ApiOperation(value = "修改商品类型",produces = "query")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "删除的id",paramType = "query"),

    })
    @PostMapping("/updateByGoodsType")
    public Map updateByGoodsType(String goodsType,Integer id){
        Integer integer = orderAddService.updateByGoodsType(goodsType, id);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCount("200");
        jsonResult.setCount("修改成功");
        return jsonResult.getMap();
    }
    @ApiOperation(value = "+/- 接口统一调用",produces = "query")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "修改的商品明细id,不是订单id",dataType = "int",paramType = "query"),

            @ApiImplicitParam(name = "bigCount",value = "修改后的大单位数量",dataType = "double",paramType = "query"),
            @ApiImplicitParam(name = "smallCount",value = "修改后的小单位数量",dataType = "double",paramType = "query"),

    })
    @PostMapping("/updateByPrice")
    public Map updateByPrice(Integer bigCount,Integer smallCount,Integer id){
        GoodsXq goodsXq = orderAddService.selectByGoodsXqId(id);
        String bigPrice=goodsXq.getBigUnit();
        int i = bigPrice.indexOf("/");
        String smallPrice=goodsXq.getSmallUnit();
        int j = smallPrice.indexOf("/");

        Double big= Double.valueOf(bigPrice.substring(0,i));
        Double small=Double.valueOf(smallPrice.substring(0,j));



        Double money=big*bigCount+small*smallCount;
        if ("赠品".equals(goodsXq.getGoodsType())){
            money=0.00;
        }
        orderAddService.updateByPrice(bigCount,smallCount,money,id);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCount("200");
        jsonResult.setCount("修改成功");
        return jsonResult.getMap();
    }
}
